package javaBase.DoubleDirectLinkList;

import javaBase.LinkList.Link;

/**
 * @program: jvm
 * @description
 * @author: tkk fendoukaoziji
 * @create: 2019-05-04 08:43
 **/
public class DoubleLinkList {
    private DoubleLink first;
    private DoubleLink last;

    public DoubleLinkList() {
        this.first = first;
        this.last = last;
    }

    public void insertFirst(long dd){
        DoubleLink newLink = new DoubleLink(dd);
        //把first节点的后置节点置为当前节点，
        if(isEmpty()){//如果是空链表 需要把last指向末尾
            last=newLink;
        }else{
            first.pre=newLink;  //newLink <-- old first
        }
        newLink.next=first;
        first=newLink;

    }
    public void insertLast(long dd){
        DoubleLink newLink = new DoubleLink(dd);
        if(isEmpty()){
            first=newLink;
        }else{
            last.next=newLink;
            newLink.pre=last;
        }
        last=newLink;
    }

    public DoubleLink deleteFirst(){
        DoubleLink temp=first;
        if(first.next==null){ //if only one item
            last=null;       //null <-- last
        }else{
            first.next.pre=null;
        }
        first=first.next;
        return  temp;
    }

    public DoubleLink deleteLast(){
        DoubleLink temp=last;
        if(first.next==null){
            first=null;
        }else{
            last.pre.next=null;
        }
        last=last.pre;
        return temp;
    }


    public  boolean insertAfterKey(long key,long dd){
        DoubleLink curr=first;
        while (curr.value!=key){
            curr=curr.next;
            if(curr==null){
                return false; //didn't find it
            }
        }
        DoubleLink newLink=new DoubleLink(dd);
        if(curr==last){
            newLink.next=null;
            last=newLink;
        }else{
             newLink.next=curr.next;
             curr.next.pre=newLink;
        }
        newLink.pre=curr;
        curr.next=newLink;
        return true;
    }

    public DoubleLink deleteKey(long key){
        DoubleLink curr=first;
        while (curr.value!=key){
            curr=curr.next;
            if(curr.next==null){
                return null;
            }
        }
        if(curr==first){
            first=curr.next;
        }else{
            curr.pre.next=curr.next;
        }
        if(curr==last){
            last=curr.pre;
        }else{
            curr.next.pre=curr.pre;
        }
        return curr;
    }


    public void dispalyForward(){
        System.out.print("List(first--last):");
        DoubleLink curr=first;
        while (curr!=null){
            curr.displayDoubleLink();
            curr=curr.next;
        }
        System.out.println("");
    }

    public  void displayBackWord(){
        System.out.print("List (last-->first: ");
        DoubleLink curr=last;
        while (curr!=null){
            curr.displayDoubleLink();
            curr=curr.pre;
        }
    }
     public boolean  isEmpty(){
        return first==null;
     }
    public DoubleLink getFirst() {
        return first;
    }

    public void setFirst(DoubleLink first) {
        this.first = first;
    }

    public DoubleLink getLast() {
        return last;
    }

    public void setLast(DoubleLink last) {
        this.last = last;
    }
}
